package org.biz.news.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import org.biz.impl.BaseBiz;
import org.biz.news.INewsBiz;
import org.hibernate.dao.IBaseDao;
import org.hibernate.entity.Column;
import org.hibernate.entity.NewsContents;
import org.hibernate.entity.NewsTitle;
import org.hibernate.entity.Suggestion;

/**
 * 
 * 新闻详情业务对象
 * 
 * @Title: NewsBiz.java
 * @Description:新闻系统(一期)
 * @Copyright: Copyright (c) 2011
 * @Company:北京学邦时代
 * @author 苗有虎
 * @date Oct 31, 2012 1:46:46 PM
 * @version V4.0
 */
@SuppressWarnings("unchecked")
public class NewsBiz extends BaseBiz implements INewsBiz {

	private IBaseDao newsDao = null;

	public IBaseDao getNewsDao() {
		return newsDao;
	}

	public void setNewsDao(IBaseDao newsDao) {
		this.newsDao = newsDao;
	}

	/**
	 * 通过标题编号查看新闻内容
	 */

	public List<NewsContents> selNewsContentsByTid(int tid, int pageNo,
			int pageSize) {
		return this.newsDao.getPageBySql(NewsContents.class,
				"select id , tid , cid ,uid, contents,posttime from news_contents  where tid="
						+ tid + " order by id", pageNo, pageSize);
	}

	/**
	 * 通过关键词查询相关的文章;
	 */
	public List<NewsTitle> selRelateArticleByKid(String keyword, int pageSize) {
		String[] keys = keyword.split(",");
		StringBuffer where = new StringBuffer();
		int count = 0;
		for (int i = 0; i < keys.length; i++) {
			count++;
			if (count != keys.length) {
				where.append(" kname='" + keys[i] + "' or ");
			} else {
				where.append(" kname='" + keys[i] + "' ");
			}
		}

		return this.newsDao.getObjectBySQL(NewsTitle.class,
				"select * from  news_title where red=0 and  id in(select tid from keywordid where "
						+ where.toString() + ") order by iftop desc,posttime desc limit "
						+ pageSize);
	}

	/**
	 * 本栏目下的相关标签;
	 */
/*	public List<Keywordid> selKeywordsByCid(int cid) {
		return this.newsDao.getObjectBySQL(Keywordid.class,
				"select * from keywordid where cid=" + cid
						+ " group by kid order by id");
	}*/

	/**
	 * 本栏目最新文章;
	 */
	public List<NewsTitle> selNewsByCidNoPic(int cid, int pageSize) {
		return this.newsDao.getObjectBySQL(NewsTitle.class,
				"select * from news_title where red=0 and  cid=" + cid
						+ " order by iftop desc,posttime desc limit " + pageSize);
	}

	/**
	 * 本栏目最新含有缩略图片的文章;
	 */
	public List<NewsTitle> selNewsByCidPic(int cid) {
		return this.newsDao.getObjectBySQL(NewsTitle.class,
				"select * from news_title where red=0 and  cid=" + cid
						+ " and ispic=1 order by iftop desc,posttime desc limit 1");
	}

	/**
	 * 查询所有的标签,并按关联文章的数量排序;
	 */
/*	public List<Keyword> selAllTags() {
		return this.newsDao.query("from Keyword order by id desc");
	}*/

	/**
	 * 查看文章评论功能;
	 */
/*	public List<Comment> selArticleComment(int tid) {

		return this.newsDao.query("from Comment where tid=" + tid
				+ " order by pubtime desc");
	}*/

	/**
	 * 统计文章评论数据
	 */
	public int doCountArticleComment(int tid) {
		return this.newsDao
				.countQuery("select count(*) from Comment where tid=" + tid);
	}

	/** (20131224 add by xy) start... */
	/**
	 * 推荐文章
	 */
	public List<NewsTitle> searchByRecommend(int limit) {

		// 推荐，不在回收站中，普通新闻
		String strHql = "from NewsTitle n where n.ifelite!=0 and n.red=0 and n.newType=0 "
				+ "order by n.ispic desc,n.ifelite desc,n.posttime desc";
		if (limit == 0) {
			return this.newsDao.query(strHql);
		} else {
			return this.newsDao.query(strHql, 1, limit);
		}
	}

	/**
	 * 咨询排行
	 */
	public List<NewsTitle> searchConsultingOrder(int intDays, int limit) {

		try {
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.DAY_OF_WEEK, intDays);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String strDate = sdf.format(calendar.getTime());
			String strHql = "";
			if(intDays == 0){
				strHql = "from NewsTitle n where n.red=0 and n.newType=0 and n.posttime<='" + strDate + "' order by n.pv desc,n.posttime desc,n.id desc";
			} else {
				strHql = "from NewsTitle n where n.red=0 and n.newType=0 and n.posttime>'"
					+ strDate + "' order by n.pv desc,n.posttime desc,n.id desc";
			}
			if (limit == 0) {
				return this.newsDao.query(strHql);
			} else {
				return this.newsDao.query(strHql, 1, limit);
			}
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 装载指定的新闻的上一篇
	 */
	public NewsTitle loadForeNews(Integer cid,Integer id) {

		System.out.println("id:" + id);
		String strHql = "from NewsTitle nt "
				+ " where nt.newType=0 and nt.red=0 and nt.cid=" + cid + " and nt.posttime >=(select t.posttime from NewsTitle t where t.id="
				+ id + ") ORDER BY nt.posttime asc,nt.id asc";
		// 如果结果只有1，则证明没有上一篇
		List<NewsTitle> lstNewsTitle = newsDao.query(strHql,1,2);
		if (lstNewsTitle != null && lstNewsTitle.size() == 2) {
			System.out.println(" fore id:" + lstNewsTitle.get(0).getId());
			return lstNewsTitle.get(1);
		} else {
			return null;
		}

	}

	/**
	 * 装载指定的新闻的下一篇
	 */
	public NewsTitle loadNextNews(Integer cid,Integer id) {

		String strHql = "from NewsTitle nt "
			+ " where nt.newType=0 and nt.red=0  and nt.cid=" + cid + "  and nt.posttime <=(select t.posttime from NewsTitle t where t.id="
			+ id + ") ORDER BY nt.posttime desc,nt.id desc";
		// 如果结果只有1，则证明没有下一篇
		List<NewsTitle> lstNewsTitle = newsDao.query(strHql,1,2);
		if (lstNewsTitle != null && lstNewsTitle.size() == 2) {
			System.out.println(" next id:" + lstNewsTitle.get(1).getId());
			return lstNewsTitle.get(1);
		} else {
			return null;
		}
	}
	/**
	 * 装载指定的问答的上一篇
	 */
	public Suggestion loadForeAnswer(Integer id) {

		System.out.println("id:" + id);
		String strHql = "select * from suggestion nt "
				+ " where nt.sign=1 and nt.create_date >=(select t.create_date from suggestion t where t.id="
				+ id + ") ORDER BY nt.create_date ,nt.id desc ";
		// 如果结果只有1，则证明没有上一篇
		List<Suggestion> lstSuggestion = newsDao.getPageBySql(Suggestion.class,strHql,1,2);
		if (lstSuggestion != null && lstSuggestion.size() == 2) {
			System.out.println(" fore id:" + lstSuggestion.get(0).getId());
			return lstSuggestion.get(1);
		} else {
			return null;
		}

	}

	/**
	 * 装载指定的问答的下一篇
	 */
	public Suggestion loadNextAnswer(Integer id) {

		String strHql = "select * from suggestion nt "
				+ " where nt.sign=1 and nt.create_date <=(select t.create_date from suggestion t where t.id="
				+ id + ") ORDER BY nt.create_date DESC ,nt.id DESC";
		// 如果结果只有1，则证明没有下一篇
		List<Suggestion> lstSuggestion = newsDao.getPageBySql(Suggestion.class,strHql,1,2);
		if (lstSuggestion != null && lstSuggestion.size() == 2) {
			System.out.println(" next id:" + lstSuggestion.get(1).getId());
			return lstSuggestion.get(1);
		} else {
			return null;
		}
	}
	
	/**
	 * 查找指定新闻的栏目
	 */
	public Column searchColumnByCid(Integer cid){
		return (Column)newsDao.loadById(Column.class, cid);
	}
	
	/**
	 * 最新文章
	 */
	public List<NewsTitle> listNewestArciles(int limit){

		String strHql = "from NewsTitle nt where nt.cid NOT in (23,54,55) AND nt.red=0 order by nt.posttime desc,nt.id desc";
		return newsDao.query(strHql, 1, limit);
	}
	/** 20131224 add by xy end... */
	
	public List<Column> listChildColumns(Column columns) {
		if (columns == null) {
			return null;
		} else {
			return this.newsDao.query("from Column as a where a.menuColumns.id="
					+ columns.getId() + " order by a.sortWay,a.id");
		}
	}
	public List<Column> listChildColumns() {
		
		return this.newsDao.query("from Column as a  order by a.sortWay,a.id");

}
}
